package com.sales.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sales.client.StaffSalesClient;
import com.sales.dao.SalesOrderMapper;
import com.sales.dao.SalesSlipMapper;
import com.sales.service.SalesSlipDetService;
import com.sales.service.SalesSlipService;
import org.comple.erp.pojo.base.enterprise.Staff;
import org.comple.erp.pojo.sales.Quotation;
import org.comple.erp.pojo.sales.SalesOrder;
import org.comple.erp.pojo.sales.SalesSlip;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

@Service
@Transactional(rollbackFor = RuntimeException.class)
public class SalesSlipServiceImpl implements SalesSlipService {
    @Autowired
    private SalesSlipMapper salesSlipMapper;
    @Autowired
    private SalesSlipDetService salesSlipDetService;
    @Autowired
    private SalesOrderMapper salesOrderMapper;

    @Override
    public PageInfo<SalesSlip> selectSlipList(Map<String, Object> params) {
        String pageNumStr = (String) params.get("pageNum");
        String pageSizeStr = (String) params.get("pageSize");

        int pageNum = pageNumStr != null && !pageNumStr.isEmpty() ? Integer.parseInt(pageNumStr) : 1;
        int pageSize = pageSizeStr != null && !pageSizeStr.isEmpty() ? Integer.parseInt(pageSizeStr) : 5;

        PageHelper.startPage(pageNum, pageSize);

        List<SalesSlip> salesSlipList = salesSlipMapper.selectSlipList(params);

        PageInfo<SalesSlip> pageInfo = new PageInfo<>(salesSlipList,5);
        return pageInfo;
    }

    @Override
    public Boolean insertSelective(SalesSlip record) {
        if(record.getIsReturn()==0){
            SalesOrder salesOrder = salesOrderMapper.selectByPrimaryKey(record.getSalesOrderId());
            salesOrder.setSalesSlipCode(record.getCode());
            if(salesOrderMapper.updateByPrimaryKeySelective(salesOrder)<=0){
                return false;
            }
        }
        if(record.getIsReturn()==1){
            SalesSlip salesSlip = salesSlipMapper.selectByPrimaryKey(record.getSalesOrderId());
            salesSlip.setReturnCode(record.getCode());
            if(salesSlipMapper.updateByPrimaryKeySelective(salesSlip)<=0){
                return false;
            }
        }

        if(salesSlipMapper.insertSelective(record) >0){
            return true;
        }
        return false;
    }

    @Override
    public Boolean updateByPrimaryKeySelective(SalesSlip record) {
        if(salesSlipMapper.updateByPrimaryKeySelective(record) >0){
            return true;
        }
        return false;
    }

    @Override
    public Boolean deleteByPrimaryKey(String id) {
        SalesSlip salesSlip = salesSlipMapper.selectByPrimaryKey(id);
        Map<String,Object> params = new HashMap<>();
        if(salesSlip.getIsReturn()==0){
            params.put("salesSlip_code", salesSlip.getCode());
        }else if(salesSlip.getIsReturn()==1){
            params.put("salesSlip_code", salesSlip.getSalesOrderCode());
        }

        if(salesSlipDetService.selectBySalesSlipCode(params) != null){
            salesSlipDetService.deleteByCodeLj(params);
        }
        if(salesSlipMapper.deleteByPrimaryKey(id) >0){
            return true;
        }
        return false;
    }

    @Override
    public SalesSlip selectByPrimaryKey(String id) {
        SalesSlip salesSlip = salesSlipMapper.selectByPrimaryKey(id);
        return salesSlip;
    }

    @Override
    public List<SalesSlip> getSaleSlipList(String customer_code) {
        List<SalesSlip> salesSlipList = salesSlipMapper.getSaleSlipList(customer_code);
        return salesSlipList;
    }

    @Override
    public List<SalesSlip> getSaleSlip(String code) {
        List<SalesSlip> salesSlipList = salesSlipMapper.getSaleSlip(code);
        return salesSlipList;
    }

    @Override
    public Boolean updateSlipList(Map<String, Object>  params) {
        if(salesSlipMapper.updateSlipList( params) >0){
            return true;
        }
        return false;
    }

    @Override
    public SalesSlip selectByCode(Map<String, Object>  params) {
        SalesSlip salesSlip = salesSlipMapper.selectByCode( params);
        return salesSlip;
    }
}
